<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">项目管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>项目管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="project-search-key">
<!--                <option value="">-请选择-</option>-->
                <option value="title">项目名称</option>
            </select>&emsp;
            <input id="project-edit-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="project-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="project-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="project-table" lay-filter="project-table"></table>
    </div>
</div>

<!-- 表单弹窗 -->
<script type="text/html" id="project-model">
    <form id="project-form" lay-filter="project-form" class="layui-form model-form">
        <input name="pro_id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">项目名称</label>
            <div class="layui-input-block">
                <input name="title" placeholder="请输入项目名" type="text" class="layui-input" maxlength="20"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input name="content" placeholder="请输入描述" type="text" class="layui-input" maxlength="20"
                       lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item model-form-footer">
            <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            <button class="layui-btn" lay-filter="project-form-submit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="project-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var formSelects = layui.formSelects;
        var apiPre = "api-scriptwriter/api/mind/project/";

        //渲染表格
        let projectTable = table.render({
            elem: '#project-table',
            url: config.base_server + apiPre + 'list',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'pro_id', sort: true, title: 'ID',width: 80},
                {field: 'title', sort: true, title: '项目名称'},
                {field: 'content', sort: true, title: '描述'},
                {field: 'createUserId', sort: true,  title: '创建人'},
                {field: 'createDate', sort: true,  title: '创建时间'},
                {align: 'center', toolbar: '#project-table-bar', title: '操作',width: 250}
            ]]
        });


        // 表单提交事件
        form.on('submit(project-form-submit)', function (data) {
            layer.load(2);
            if (data.field.pro_id){
                admin.req(apiPre + 'update', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    console.log(data);
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        table.reload('project-table');
                        layer.closeAll('page');
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'PATCH');
            }else{
                admin.req(apiPre + 'add', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    console.log(data);
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        table.reload('project-table');
                        layer.closeAll('page');
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'POST');
            }

            return false;
        });

        // 工具条点击事件
        table.on('tool(project-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }
        });

        // 添加按钮点击事件
        $('#project-btn-add').click(function () {
            showEditModel();
        });

        // 搜索按钮点击事件
        $('#project-btn-search').click(function () {
            var key = $('#project-search-key').val();
            var value = $('#project-edit-value').val();
            var data = {};
            data[key] = value;
            data["access_token"] = config.getToken().access_token
            console.log(data)
            // table.reload('project-table', {where: data});
            projectTable.reload({where: data});
        });

        // 显示编辑弹窗
        var showEditModel = function (data) {
            layer.open({
                type: 1,
                title: data ? '修改' : '添加',
                area: '450px',
                offset: '120px',
                content: $('#project-model').html(),
                success: function () {
                    $('#project-form')[0].reset();
                    $('#project-form').attr('method', 'POST');
                    if (data) {
                        // $("input[name='projectKey']").attr('disabled',true);
                        // $("input[name='projectKey']").attr('class',"layui-input layui-disabled");
                        form.val('project-form', data);
                        $('#project-form').attr('method', 'POST');
                    }
                }
            });
        };

        // 删除
        var doDelete = function (obj) {
            layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req(apiPre + 'del/' + obj.data.pro_id, {}, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        obj.del();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'DELETE');
            });
        };


    });

</script>